import 'package:aitao_flutter/common_ui/back_nav.dart';
import 'package:aitao_flutter/common_ui/loading_view.dart';
import 'package:aitao_flutter/const/common_const.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class AtWebViewPage extends StatefulWidget {
  const AtWebViewPage({super.key});
  @override
  State<AtWebViewPage> createState() => _AtWebViewPageState();
}

class _AtWebViewPageState extends State<AtWebViewPage> {
  String url = '';
  String title = '加载中';
  WebViewController? webViewController;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BackNavigation(title: title),
        body: url == ''
            ? const LoadingView()
            : WebView(
                onWebViewCreated: (c) {
                  webViewController = c;
                },
                onPageFinished: (url) {
                  webViewController?.getTitle().then((value) {
                    setState(() {
                      title = value ?? CommonStringConst.appName;
                    });
                  });
                },
                initialUrl: url,
                javascriptMode: JavascriptMode.unrestricted,
              ));
  }

  @override
  void initState() {
    super.initState();
    if (Get.parameters.isNotEmpty) {
      url = Get.parameters['url'].toString();
      if (url != '') {
        setState(() {});
      }
    }
  }
}
